package com.rachel.activitytest.activity;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.SurfaceTexture;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.rachel.activitytest.R;

import java.io.IOException;

public class TextureViewActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, MediaPlayer.OnPreparedListener, MediaPlayer.OnCompletionListener, MediaPlayer.OnBufferingUpdateListener {
    private static final String TAG = "zsr";
    private TextureView mTextureView;
    private Surface mSurface;
    private MediaPlayer mMediaPlayer;
    private int mScreenWidth,mDestationHeight;
    public static float VIDEO_HEIGHT_PERCENT = 9 / 16.0f;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_texture_view);
        mTextureView = (TextureView) findViewById(R.id.videosdk_textureview);
        initData();
        mTextureView.setSurfaceTextureListener(this);

    }

    private void initData() {
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mDestationHeight = (int) (mScreenWidth * VIDEO_HEIGHT_PERCENT);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mScreenWidth, mDestationHeight);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        mTextureView.setLayoutParams(params);
    }

    /**
     * textureview 准备阶段
     * @param surface
     * @param width
     * @param height
     */
    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Log.d(TAG, "onSurfaceTextureAvailable: ");
        mSurface = new Surface(surface);
        try {
            mMediaPlayer = new MediaPlayer();
            AssetFileDescriptor afd = getAssets().openFd("textvideo.mp4");
            mMediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
            mMediaPlayer.setSurface(mSurface);
            mMediaPlayer.prepare();

            mMediaPlayer.setOnPreparedListener(this);
            mMediaPlayer.setOnCompletionListener(this);
            mMediaPlayer.setOnBufferingUpdateListener(this);
            
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            
           // mMediaPlayer.play();


        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "onSurfaceTextureAvailable: "+e.toString());
        }

    }
    public void play(View view){
        if (mMediaPlayer != null){
            mMediaPlayer.start();
        }
    }
    public void pause(View view){
        if (mMediaPlayer != null){
            mMediaPlayer.pause();
        }
    }
    
    // 发生改变时调用
    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }
    // 销毁
    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }
    // 刷新
    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        Log.d(TAG, "onPrepared: ");
    }   

    @Override
    public void onCompletion(MediaPlayer mp) {
        Log.d(TAG, "onCompletion: ");
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {
        Log.d(TAG, "onBufferingUpdate: ");
    }
}
